import {download} from "@/utils/ossUtil";
import cloud from '@lafjs/cloud'

// @ts-ignore
const db = cloud.database()

const mongo = cloud.mongo.db
// @ts-ignore
async function importRegion() {
  let res = await download("dbmvlq-init","region.json")
  //1.读取数据格式
  let dataStr = await res.Body.transformToString()
  //2.转换为对象
  let data = JSON.parse(dataStr)
  console.log(data)
  //3.转换数据即可
  let saveData = data.RECORDS.map(item=>{
    return  {
      _id:item.id,
      parentId:item.parent_id,
      name:item.name,
      createTime:item.create_time,
      updateTime:item.update_time,
      deleted:0
    }
  })

  await mongo.collection("region").insertMany(saveData)

}

async function importLeader() {
  let res = await download("dbmvlq-init","leader.json")
  //1.读取数据格式
  let dataStr = await res.Body.transformToString()
  //2.转换为对象
  let data = JSON.parse(dataStr)
  console.log(data)
  //3.转换数据即可
  let saveData = data.RECORDS.map(item=>{
    function gettakeType(take_type: any) {
      let num = 1
      switch (take_type) {
        case '1':
          num = 1
          break
        case '2':
          num = 2
          break
        case '3':
          num = 3
          break
        case '4':
          num = 4
          break
      }
      return num
    }

    function getHaveStore(have_store: any) {
      let num = 1
      switch (have_store) {
        case '-1':
          num = -1
          break
        case '0':
          num = 0
          break
        case '1':
          num = 1
          break
        case '2':
          num = 2
          break
        case '3':
          num = 3
          break
        case '4':
          num = 4
          break
      }
      return num
    }

    return  {
      _id:item.id,
      userId:item.user_id,
      regionId:item.region_id,
      name:item.name,
      phone:item.phone,
      idNo:item.id_no,
      idNoUrl1:item.id_no_url1,
      idNoUrl2:item.id_no_url2,
      takeName:item.take_name,
      takeType:gettakeType(item.take_type),
      province:item.province,
      city:item.city,
      district:item.district,
      detailAddress:item.detail_address,
      longitude:item.longitude,
      latitude:item.latitude,
      haveStore:getHaveStore(item.have_store),
      storePath:item.store_path,
      workTime:item.work_time,
      workStatus:getHaveStore(item.work_status),
      checkStatus:getHaveStore(item.check_status),
      checkTime:item.check_time,
      checkUser:item.check_user,
      checkContent:item.check_content,
      createTime:item.create_time,
      updateTime:item.update_time,
      deleted:0
    }
  })

  await mongo.collection("leader").insertMany(saveData)

}

async function importUserDelivery() {
  let res = await download("dbmvlq-init","user_delivery.json")
  //1.读取数据格式
  let dataStr = await res.Body.transformToString()
  //2.转换为对象
  let data = JSON.parse(dataStr)
  console.log(data)
  //3.转换数据即可
  let saveData = data.RECORDS.map(item=>{
    return  {
      _id:item.id,
      userId:item.user_id,
      leaderId:item.leader_id,
      wareId:item.ware_id,
      isDefault:item.is_default==='1'?1:0,
      createTime:item.create_time,
      updateTime:item.update_time,
      deleted:0
    }
  })

  await mongo.collection("userDelivery").insertMany(saveData)

}

// @ts-ignore
export default async function (ctx: FunctionContext) {
  await importLeader()
  await importUserDelivery()
}

